package com.sailmi.auth.userprovider.ucs;
import com.sailmi.common.model.auth.AuthUser;
import com.sailmi.common.secure.user.IUserProvider;
import com.sailmi.common.tool.api.R;
import com.sailmi.common.tool.utils.BeanUtil;
import com.sailmi.system.entity.UserInfo;
import com.sailmi.system.feign.IUserClient;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
public class UserProvider implements IUserProvider {
	private static final Logger log = LoggerFactory.getLogger(UserProvider.class);
    @Resource
	IUserClient userClient;

    public AuthUser userInfo(Long userId) {
        log.debug("Now get user info "+userId );
        R<UserInfo> userInfo=userClient.userInfo(userId);
		AuthUser authUser = new AuthUser();
		BeanUtil.copyProperties(userInfo.getData(),authUser);
		authUser.setUserId(userInfo.getData().getUser().getId());
		log.debug("Yes Get User from remote:"+userInfo);
        return authUser;
    }

	@Override
	public AuthUser userInfo(String userAccount) {
		return null;
	}

	@Override
	public AuthUser registerUser(AuthUser authUser) {
		return null;
		//return userClient.registerUser(authUser).getData();
	}

	@Override
	public AuthUser updateUser(AuthUser authUser) {
		return null;
	}

	public AuthUser userInfo(String tenantId, String account, String password) {
        log.info("Now in UserProvider for sailmi................................."+account);
        R<UserInfo> userInfoRes=userClient.userInfo(tenantId,account, password);
		UserInfo userInfo = userInfoRes.getData();
		log.info("Now in UserProvider for sailmi................................."+userInfo);
		AuthUser auth =new AuthUser();
		log.info("Now in UserProvider for sailmi................................."+userInfo.toString());
		if(Objects.nonNull(userInfo)){
			auth.setAccount(userInfo.getUser().getAccount());
			auth.setAvatar(userInfo.getUser().getAvatar());
			auth.setUserName(userInfo.getUser().getRealName());
			auth.setRoles(userInfo.getRoles());
			auth.setUserCode(userInfo.getUser().getCode());
			auth.setEnterprise(userInfo.getUser().getDefaultEnterpriseId().toString());
			auth.setTenantId(userInfo.getUser().getTenantId());
			auth.setUserId(userInfo.getUser().getId());
			auth.setPassword(userInfo.getUser().getPassword());
			auth.setPermissions(userInfo.getPermissions());
			auth.setPhone(userInfo.getUser().getPhone());
			auth.setNickName(userInfo.getUser().getName());
			log.info("Now in UserProvider for sailmi................................."+ auth);
			return auth;
		}else{
			return null;
		}
    }

    public Long submitUserInfo(AuthUser user) {
        return null;
    }

    public Boolean resetUserPass(String id) {
        return null;
    }
}
